import {
  spaList
} from '@/api/spa'
import {
  getgemomemberuser
} from '@/api/my'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUri: getApp().globalData.imgUri,
    cdn: getApp().globalData.cdn,
    bottomInset: 20,
    swiperIndex: 0,
    list: [],
    spaId: 1,
    lock: false,
    showPage2: false,
    startY: 0, // 记录开始触摸的位置
    endY: 0, // 记录结束触摸的位置
  },
  handleTouchStart: function (e) {
    this.setData({
      startY: e.touches[0].pageY
    });
  },
  handleTouchMove: function (e) {
    // 可以在这里添加触摸移动时的处理逻辑
    this.setData({
      endY: e.touches[0].pageY
    });
  },
  handleTouchEnd: function (e) {
    const startY = this.data.startY;
    const endY = this.data.endY;
    if (endY - startY > 50) { // 假设滑动超过50px则认为是下滑
      console.log('向下滑动');
      this.setData({
        showPage2: false
      })
    } else if (startY - endY > 50) { // 假设滑动超过50px则认为是上滑
      console.log('向上滑动');
      this.setData({
        showPage2: true
      })
    }
  },
  swiperChange(e) {
    this.setData({
      swiperIndex: e.detail.current
    })
  },
  topage() {
    let t = this;
    console.log(this.data.lock)
    if (this.data.lock) return
    t.setData({
      spaId: t.data.spaId,
      lock: true
    });
    wx.getSetting({
      success: (res) => {
        console.log(res,'sssss')
        let authSetting = res.authSetting;
        if (authSetting['scope.userFuzzyLocation'] == false) {
          wx.showModal({
            title: '您未开启地理位置授权',
            content: '请授权获取地理位置',
            success: res => {
              if (res.confirm) {
                wx.openSetting();
              }
            }
          });
        } else {
          wx.getFuzzyLocation({
            type: 'wgs84',
            success(res) {
              const latitude = res.latitude;
              const longitude = res.longitude;

              wx.setStorageSync('latitude', latitude);
              wx.setStorageSync('longitude', longitude);
              wx.navigateTo({
                url: '/pages/selStory/selStory?&spaId=' + t.data.spaId + '&latitude=' + latitude + '&longitude=' + longitude,
              })
            }
          })
        }
      },
      complete:(com)=>{
        console.log(com)
      }
    })



  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let t = this;
    const systemInfo = wx.getWindowInfo();
    const bottomInset = Math.abs(systemInfo.safeArea.bottom - systemInfo.windowHeight);
    console.log('Bottom inset: ', bottomInset);
    t.setData({
      bottomInset,
      lock:false
    });

    if (options.id) {
      var spaId = options.id;
      t.setData({
        spaId: spaId
      });
      spaList().then(res => {
        if (res.code == 200) {
          let data = {};
          for (var i = 0; i < res.data.length; i++) {
            if (spaId == res.data[i].id) {
              data = res.data[i];
            }
          }
          var introduceImage = data.introduceImage;
          var list = introduceImage.split(",");
          t.setData({
            list: list
          });

          if(options.utm_source){
            wx.setStorageSync('utm_source', options.utm_source);
            getgemomemberuser({}).then(res => {
              if (res.code == 200) {
                wx.setStorageSync('getgemomemberuser', res.data);
                if(res.data.phonenumber){
                  wx.setStorageSync('phone', res.data.phonenumber);
                }else{
                  wx.setStorageSync('module_url','/pages/spa/spa?id='+options.id+'&utm_source='+options.utm_source);
                  wx.reLaunch({
                    url:'/pages/register/register'
                  });
                }
              }
            });
          }else{
            wx.removeStorageSync('utm_source');
          }
        }
      });
    }


  },
  onShow(){
    this.setData({
      lock: false
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: getApp().globalData.share_title,
      path: getApp().globalData.share_url,
      imageUrl: getApp().globalData.share_image,
    }
  }
})